
var bar_width = '300px';
var bar_height = '30px';

var btn_width = '50px';

/**
 * 全局提交参数对象
 */
//var globleParams = null;

function convertIntoInt(len) {
	var i = len.indexOf('px');
	return len.substring(0, i);
}

function getleftDis(allDis, count) {
	var len = convertIntoInt(allDis)/(count );
	var magr = len - convertIntoInt(btn_width);
	return magr ;
}

var ToolBar = Class.create();
Object.extend(ToolBar.prototype, Observable);
Object.extend(ToolBar.prototype, {
	initialize : function(objs) {
		this.objs = objs;
		this.createElems();
	},
	createElems : function() {
		var len = this.objs.length;
		this.bar = this.createDiv();
		
		var unitDis = getleftDis(bar_width, len);
		for (i = 0; i < len; i++) {
			var obj = this.objs[i];
			//alert((i+1)*unitDis + i*convertIntoInt(btn_width) + 'px');
			var btn = this.createBtn('btn'+i, obj.action, obj.name, obj.order, obj.value, unitDis + 'px');
			this.bar[i] = btn;
			this.bar.appendChild(btn);
		}
		
		
	},
	createDiv : function() {
		var bar = ffC('div');
		//bar.style.position = 'absolute';
		//bar.style.zIndex = 1;
		bar.style.width = bar_width;
		bar.style.height = bar_height;
		bar.style.background = '#aaccdd';
		document.body.appendChild(bar);
		return bar;
	},
	createBtn : function(id, action, name, order, value, magr) {
		var btn = ffC('input');
		btn.type = 'button';
		btn.id = id;
		btn.action = action;
		btn.name = name;
		btn.value = value;
		btn.order = order;
		//alert('111');
		btn.style.width = btn_width;
		btn.style.marginLeft = magr;
		btn.toolBar = this;
		btn.onclick = function() {
			this.toolBar.btnClick.bind( this )();
		};
		return btn;
	},
	getDiv : function() {
		return this.bar;
	},
	getParamsObj : function() {
		//由外部重写覆盖此方法 ,返回参数对象值
		// 如：globleParams = {'name':'tom','psd':'123'}; return globleParams;
	},
	processing : function() {
		//alert('processing......');
		this.dicv = createDiscoverDiv();
	},
	btnClick : function() {
		this.toolBar.processing();
		var params = {'par':this.name};
		var ajaxUse = Ajax.getTransport();
		ajaxUse.sendRequest(this.action, params, 'post');
		ajaxUse.onSuccess = this.toolBar.success.bind(this);
	},
	success : function(data) {
		//alert(this.toolBar.bar.parentNode);
		this.toolBar.whenSuccess(data);
		this.toolBar.bar.parentNode.removeChild(this.toolBar.bar);
		var obs = JSON.parse(data);
		new ToolBar(obs);
	},
	whenSuccess : function(data) {
		//由外部重写覆盖此方法,执行ajax返回后的动作
		//alert(this.dicv);
		createDiscoverOkBtn(this.dicv);
	}
});

var StartRes = Class.create();
Object.extend(StartRes.prototype, {
	initialize : function(url) {
		//this.sendAjax(url);
		this.url = url;
	},
	sendAjax : function() {
		var ajaxUse = Ajax.getTransport();
		ajaxUse.sat = this;
		ajaxUse.sendRequest(this.url, null, 'post');
		ajaxUse.onSuccess = this.success;
	},
	success : function(data) {
		//var objs = JSON.parse(data);
		//this.sat.toolbar = new ToolBar(objs);
		//alert(this.toolbar);
	},
	getToolBar : function() {
		return this.toolbar;
	}
});

function createDiscoverDiv() {
	var width = document.body.clientWidth;//window.screen.availWidth;
	var height = document.body.clientHeight;//window.screen.availHeight;
	
	var discover = ffC('div');
	discover.style.position = 'absolute';
	discover.style.top = '0px';//window.screenTop;
	discover.style.left = '0px';//window.screenLeft;
	discover.style.width = width;
	discover.style.height = height;
	discover.style.background = '#ffffff';
	discover.style.filter="Alpha(Opacity=50)";
	discover.style.MozOpacity="0.5";
	discover.style.zIndex = 900;
	document.body.appendChild(discover);
	
	var pmt = ffC('div');
	pmt.style.position = 'absolute';
	pmt.style.zIndex = 901;
	pmt.style.top = '240px';
	pmt.style.left = '440px';
	pmt.style.width = '280px';
	pmt.style.height = '100px';
	pmt.style.background = '#ffffff';
	//pmt.innerHTML = '<center>please waiting...</center>';
	pmt.style.borderTop = '#000000 2px solid';
	pmt.style.borderLeft = '#000000 2px solid';
	pmt.style.borderRight = '#000000 2px solid';
	pmt.style.borderBottom = '#000000 2px solid';
	discover.appendChild(pmt);
	
	var img = ffC('img');
	img.style.position = 'absolute';
	img.style.top = '30px';
	img.style.left = '60px';
	img.style.width = '160px';
	img.style.height = '15px';
	img.style.zIndex = 902;
	img.src = '/MyTestWeb/js/img/load2.gif';
	pmt.appendChild(img);
	
	discover.pmt = pmt;
	pmt.inn = img;
	return discover;
}

function createDiscoverOkBtn(disv) {
	disv.pmt.removeChild(disv.pmt.inn);
	disv.pmt.innerHTML = "<center>execute sucess!!!</center>";
	var btn = ffC('input');
	btn.type = 'button';
	btn.value = 'OK';
	btn.style.position = 'absolute';
	btn.style.zIndex = 902;
	btn.style.top = '50px';
	btn.style.left = '100px';
	btn.style.width = '50px';
	btn.style.height = '30px';
	btn.disv = disv;
	btn.onclick = removeDisv;
	disv.pmt.appendChild(btn);
}

function removeDisv() {
	this.disv.parentNode.removeChild(this.disv);
}

function startToolBar() {
	var toolbar = null;
	var st = new StartRes('http://127.0.0.1:8080/MyTestWeb/MyServlet');

	st.success = function(data) {
		var objs = JSON.parse(data);
		new ToolBar(objs);
	};
	st.sendAjax();
}